using System;
using System.ComponentModel;
using System.IO;
using RollManager.Library.Utils;

namespace RollManager.Library.ActionSteps
{
    [Serializable]
    public class DeleteFilesStep : ActionStep
    {
        private string m_destDir = StepConstants.PATH;
        private string m_filter = StepConstants.FILTER;
        private bool m_recursive = false;

        [DisplayName( "Destination Dir" )]
        [Description( "The directory path from which files are deleted." )]
        public string DestDir
        {
            get
            {
                return m_destDir;
            }
            set
            {
                if( string.IsNullOrEmpty( value ) )
                {
                    throw new ArgumentException( "Destination Dir cannot be empty." );
                }
                else
                {
                    m_destDir = value.Trim();
                }
            }
        }

        [DisplayName( "File Filter" )]
        [Description( "The file filter to use when deleting files from in the destination directory." )]
        public string Filter
        {
            get
            {
                return m_filter;
            }
            set
            {
                if( string.IsNullOrEmpty( value ) )
                {
                    throw new ArgumentException( "Filter cannot be empty.  Use *.* for a global wildcard." );
                }
                else
                {
                    m_filter = value.Trim();
                }
            }
        }

        [Description( "Whether the delete should only apply to the top level destination directory or to all children directories as well." )]
        public bool Recursive
        {
            get
            {
                return m_recursive;
            }
            set
            {
                m_recursive = value;
            }
        }

        public override string Name
        {
            get { return "Delete Files"; }
        }

        protected override void Run()
        {
            DirectoryHelper helper = new DirectoryHelper( Progress );
            helper.Delete( m_destDir, m_filter, m_recursive );
        }

        protected override void Simulate()
        {
            // Check for dest dir
            DirectoryInfo dir = new DirectoryInfo( m_destDir );
            if( !dir.Exists )
            {
                throw new Exception( string.Format( "Destination dir {0} does not exist.", m_destDir ) );
            }

            if( string.IsNullOrEmpty( m_filter ) )
            {
                throw new Exception( "Filter was empty." );
            }

            Progress.AddFormat( "Simulated delete of files on {0} with filter {1}", m_destDir, m_filter );
        }
    }
}
